home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / APXMDIDV.PAK / APXPRINT.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  5KB  |  163 lines

  1. //----------------------------------------------------------------------------
  2. //  Project ApxSdi
  3. //  Borland International
  4. //  Copyright ⌐ 1996. All Rights Reserved.
  5. //
  6. //  SUBSYSTEM:    ApxSdi Application
  7. //  FILE:         apxprint.cpp
  8. //  AUTHOR:
  9. //
  10. //  OVERVIEW
  11. //  ~~~~~~~~
  12. //  Source file for implementation of Printing.
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <owl/pch.h>
  16. #include "apxprint.h"
  17.  
  18.  
  19.  
  20. void TApxPrintout::GetDialogInfo(int& minPage, int& maxPage,
  21.                                  int& selFromPage, int& selToPage)
  22. {
  23.   // Calculate the total number of pages in the document. TotalPages is
  24.   // initialized in INT_MAX in the event that we cannot determine the
  25.   // number for ourselves.
  26.   //
  27.   TPrintDC* printDC = new TPrintDC( Printer->GetSetup().GetDriverName(),
  28.                                     Printer->GetSetup().GetDeviceName(),
  29.                                     Printer->GetSetup().GetOutputName(),
  30.                                     Printer->GetSetup().GetDevMode() );
  31.  
  32.   TEditFile* efWindow = TYPESAFE_DOWNCAST( Window, TEditFile );
  33.  
  34.   if( printDC && efWindow )  {
  35.     TSize pageSize( printDC->GetDeviceCaps(HORZRES),
  36.                     printDC->GetDeviceCaps(VERTRES) );
  37.  
  38.     TEXTMETRIC tm;
  39.     printDC->GetTextMetrics( tm );
  40.     int TextHeight = tm.tmHeight + tm.tmInternalLeading;
  41.  
  42.     int LinesPerPage = pageSize.cy / TextHeight;
  43.     int TotalLines = efWindow->GetNumLines();
  44.     TotalPages = TotalLines / LinesPerPage + 1; //Add one to account for any roundoff.
  45.     if(TotalPages <= 0)
  46.       TotalPages = 1;
  47.   }
  48.  
  49.   delete printDC;
  50.  
  51.   minPage = 1;
  52.   maxPage = TotalPages;
  53.   selFromPage = 0;
  54.   selToPage = 0;
  55. }
  56.  
  57.  
  58.  
  59. void TApxPrintout::BeginPage(TRect& clientR)
  60. {
  61.   TScreenDC screenDC;
  62.   TSize screenRes(screenDC.GetDeviceCaps(LOGPIXELSX),
  63.           screenDC.GetDeviceCaps(LOGPIXELSY));
  64.   TSize printRes(DC->GetDeviceCaps(LOGPIXELSX),
  65.            DC->GetDeviceCaps(LOGPIXELSY));
  66.  
  67.   // Temporarily change the window size (so any WM_PAINT queries on the total window size (GetClientRect) is
  68.   // the window size for the WM_PAINT of the window and the printer page size when Paint is called from
  69.   // PrintPage. Notice, we don't use AdjustWindowRect because its harder and not accurate.  Instead we
  70.   // compute the difference (in pixels) between the client window and the frame window.  This difference
  71.   // is then added to the clientRect to compute the new frame window size for SetWindowPos.
  72.   //
  73.   clientR = Window->GetClientRect();
  74.   Window->MapWindowPoints(HWND_DESKTOP, (TPoint*)&clientR, 2);
  75.  
  76.   // Compute extra X and Y pixels to bring a client window dimensions to equal the frame window.
  77.   //
  78.   OrgR = Window->GetWindowRect();
  79.   int adjX = OrgR.Width() - clientR.Width();
  80.   int adjY = OrgR.Height() - clientR.Height();
  81.  
  82.   // Conditionally scale the DC to the window so the printout will resemble the window.
  83.   //
  84.   if (Scale) {
  85.     clientR = Window->GetClientRect();
  86.     PrevMode = DC->SetMapMode(MapMode);
  87.     DC->SetViewportExt(PageSize, &OldVExt);
  88.  
  89.     // Scale window to logical page size (assumes left & top are 0)
  90.     //
  91.     clientR.right = MulDiv(PageSize.cx, screenRes.cx, printRes.cx);
  92.     clientR.bottom = MulDiv(PageSize.cy, screenRes.cy, printRes.cy);
  93.  
  94.     DC->SetWindowExt(clientR.Size(), &OldWExt);
  95.   }
  96.  
  97.   // Compute the new size of the window based on the printer DC dimensions.
  98.   // Resize the window, notice position, order, and redraw are not done the window size changes but the user
  99.   // doesn't see any visible change to the window.
  100.   //
  101.   Window->SetRedraw(false);
  102.   Window->SetWindowPos(0, 0, 0, clientR.Width() + adjX, clientR.Height() + adjY,
  103.                        SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER| SWP_NOACTIVATE);
  104. }
  105.  
  106.  
  107. void TApxPrintout::PrintPage(int page, TRect& bandRect, unsigned)
  108. {
  109.   TRect clientR;
  110.  
  111.   BeginPage(clientR);
  112.  
  113.   if (Scale)
  114.     DC->DPtoLP(bandRect, 2);
  115.  
  116.   // Change the printer range to this current page.
  117.   //
  118.   TPrintDialog::TData& printerData = Printer->GetSetup();
  119.   int fromPg = printerData.FromPage;
  120.   int toPg = printerData.ToPage;
  121.  
  122.   printerData.FromPage = page;
  123.   printerData.ToPage = page;
  124.  
  125.   // Call the window to paint itself to the printer DC.
  126.   //
  127.   Window->Paint(*DC, false, bandRect);
  128.  
  129.   printerData.FromPage = fromPg;
  130.   printerData.ToPage = toPg;
  131.  
  132.   if (Scale)
  133.     DC->LPtoDP(bandRect, 2);
  134.  
  135.   EndPage();
  136. }
  137.  
  138.  
  139. void TApxPrintout::EndPage()
  140. {
  141.   // Resize to original window size, no one's the wiser.
  142.   //
  143.   Window->SetWindowPos(0, 0, 0, OrgR.Width(), OrgR.Height(),
  144.                SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER| SWP_NOACTIVATE);
  145.   Window->SetRedraw(true);
  146.  
  147.   // Restore changes made to the DC
  148.   //
  149.   if (Scale) {
  150.     DC->SetWindowExt(OldWExt);
  151.     DC->SetViewportExt(OldVExt);
  152.     DC->SetMapMode(PrevMode);
  153.   }
  154. }
  155.  
  156.  
  157. bool TApxPrintout::HasPage(int pageNumber)
  158. {
  159.   TPrintDialog::TData& printerData = Printer->GetSetup();
  160.  
  161.   return pageNumber >= printerData.MinPage && pageNumber <= printerData.MaxPage;
  162. }
  163.